package com.jiaai.web.live;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.auth.model.AccessUser;
import com.jiaai.exception.ErrorCode;
import com.jiaai.exception.ServerException;
import com.jiaai.filter.aop.TokenIntercept;
import com.jiaai.live.api.IAnchorApi;
import com.jiaai.member.model.BMemberIdentity;
import com.jiaai.util.RequestUtils;
import com.jiaai.util.RestResultGenerator;
import com.jiaai.util.code_pack.TipsProperties;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title AnchorController
 * @since 2017/10/29
 */
@RequestMapping("/api/v1/app/anchor")
@RestController
public class AnchorController {

    @Reference
    private IAnchorApi anchorApi;

    /**
     * @return
     * @throws ServerException
     */
    @GetMapping("/check")
    @TokenIntercept(checkVisitor = true)
    public  Object checkAnchor()throws  ServerException{
        AccessUser accessUser = RequestUtils.getUser();

        if(accessUser==null|| StringUtils.isEmpty(accessUser.getUserId())){
            return  RestResultGenerator.genErrorResult(ErrorCode.NOT_LOGIN,"not login");
        }else{
            Integer status=anchorApi.checkAnchor(accessUser.getUserId());
            switch (status){
                case ErrorCode.NOT_IS_ANCHOR:
                    return  RestResultGenerator.genErrorResult(ErrorCode.NOT_IS_ANCHOR,TipsProperties.getTip("NOT_IS_ANCHOR"));
                case ErrorCode.ON_PROHIBIT_LIVE:
                    return  RestResultGenerator.genErrorResult(ErrorCode.ON_PROHIBIT_LIVE,TipsProperties.getTip("ON_PROHIBIT_LIVE"));
                case ErrorCode.ON_ANCHOR_CHECK:
                    return  RestResultGenerator.genErrorResult(ErrorCode.ON_ANCHOR_CHECK,TipsProperties.getTip("ON_ANCHOR_CHECK"));
                case ErrorCode.ANCHOR_REJECT:
                    return  RestResultGenerator.genErrorResult(ErrorCode.ANCHOR_REJECT,TipsProperties.getTip("ANCHOR_REJECT"));
                case ErrorCode.IDENTIFYING:
                    return RestResultGenerator.genErrorResult(ErrorCode.IDENTIFYING, TipsProperties.getTip("IDENTIFYING"));
                case ErrorCode.IDENTIFY_REJECTED:
                    return RestResultGenerator.genErrorResult(ErrorCode.IDENTIFY_REJECTED, TipsProperties.getTip("IDENTIFY_REJECTED"));
                    default:
                            return RestResultGenerator.genSuccessResult();
            }

        }
    }
    //实名认证
    @PostMapping("/identity")
    @TokenIntercept(checkVisitor = true)
    public Object identity(BMemberIdentity identity)throws  ServerException{
        AccessUser accessUser = RequestUtils.getUser();
        anchorApi.identity(accessUser.getUserId(),identity);
        return  RestResultGenerator.genSuccessResult("提交成功");
    }

    /**
     * 搜索页面的推荐
     * @return
     */
    @GetMapping("/searchAnchor")
    public Object searchAnchor(String key){
        if (!StringUtils.isEmpty(key)){
            return RestResultGenerator.genSuccessResult(anchorApi.searchAnchor(key));
        }
        return RestResultGenerator.genSuccessResult(anchorApi.recommand());
    }

    /**
     * 主播排行榜
     * @param type
     * @return
     * @throws ServerException
     */
    @GetMapping("/ranking")
    public  Object ranking( String type)throws  ServerException{
        return RestResultGenerator.genSuccessResult(anchorApi.findRanking(type));
    }



    
}
